<?php
	$order_id = $params[0];
	if($_SERVER["REQUEST_METHOD"]=='POST') {
		$sql = "SELECT `h`.`item_id` FROM `tbl_buy_history` AS `h` LEFT JOIN `tbl_buy_order` AS `o` ON `h`.`order_id` = `o`.`order_id` WHERE `h`.`order_id` = '$order_id' AND `o`.`status` = '0' ;";
		$query = mysql_query($sql);
		while($result = mysql_fetch_assoc($query)) {
			$item_id	= $result['item_id'];
			$sqlupdate	= "UPDATE `tbl_product_item` SET `count` = (`count`-1) WHERE `item_id` = '$item_id' LIMIT 1;";
			mysql_query($sqlupdate) or die($sqlupdate);
		}
		mysql_free_result($query);
		
		$ems_id		= strip_tags($_POST['ems_id']);
		$sql = "UPDATE `tbl_buy_order` SET `ems_id` = '$ems_id' ,`status` = '1' WHERE `order_id` = '$order_id' LIMIT 1;";
		mysql_query($sql);	
		redirect('../');
		exit;
	}
	
	$sql = "SELECT `o`.* , `m`.`first_name` , `m`.`last_name` , `m`.`email` FROM `tbl_buy_order` AS `o` LEFT JOIN `tbl_member` AS `m` ON `o`.`member_id` = `m`.`member_id` WHERE `order_id` = '$order_id' LIMIT 1";
	$query = mysql_query($sql);
	while($result = mysql_fetch_assoc($query)) {
		$name		= $result['first_name'].' '.$result['last_name'];
		$email		= $result['email'];
		$ems_id		= $result['ems_id'];
		$price_total= $result['price_total'];
		$date		= $result['date'];
	}
	mysql_free_result($query);
 
	$sql = "SELECT `h`.*,`p`.`title` , `p`.`price` FROM `tbl_buy_history` AS `h` LEFT JOIN `tbl_product` AS `p` ON `h`.`product_id` = `p`.`product_id` WHERE `h`.`order_id` = '$order_id' AND `p`.`status` = '1' ORDER BY `date` ASC";
	$query = mysql_query($sql) or die($sql);
	$tp->Block('list');
	while($result = mysql_fetch_assoc($query)) {
		$p_title	= $result['title'];
		$p_price	= number_format($result['price'],2);
		
		extract(select('tbl_color','color_id',$result['color_id'],""), EXTR_PREFIX_ALL , "c");
		extract(select('tbl_size','size_id',$result['size_id'],""), EXTR_PREFIX_ALL , "s");

		$p_color	= $c_title;
		$p_size		= $s_title;
		$p_count	= (int)$result['count'];
		$p_date	= $result['date'];
		$n++;
		$tp->Apply();
	}
	mysql_free_result($query);
?>